using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;

public partial class RSS : System.Web.UI.Page
{
    private string _title = "The Forecast Exchange RRS feed"; //TODO: should be configurable
    private string _description = ""; //TODO: should be configurable

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ContentType = "application/xml";

        XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        
        // initialize RSS XML
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("rss");
        xmlWriter.WriteAttributeString("version", "2.0");
        xmlWriter.WriteStartElement("channel");
        xmlWriter.WriteElementString("title", _title);
        xmlWriter.WriteElementString("link", Request.Url.AbsoluteUri.ToLower().Replace("rss.aspx", "news.aspx")); // TODO!!!
        xmlWriter.WriteElementString("description", _description);

        IEnumerator newsItems = AccessDataSource1.Select(new DataSourceSelectArguments()).GetEnumerator();
        while (newsItems.MoveNext())
        {
            DataRowView row = (DataRowView) newsItems.Current;

            xmlWriter.WriteStartElement("item");
            xmlWriter.WriteElementString("title", row["Title"].ToString());
            xmlWriter.WriteElementString("description", row["News_message"].ToString().Replace("\n", "<br/>"));
            xmlWriter.WriteElementString("pubDate", ((DateTime)row["News_date"]).ToString("r"));
            //xmlWriter.WriteElementString("guid", row["ID"].ToString());
            xmlWriter.WriteElementString("link", Request.Url.AbsoluteUri.ToLower().Replace("rss.aspx", "news.aspx")); // TODO!!!
            xmlWriter.WriteEndElement();
        }

        // finilize RSS XML and send
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndDocument();
        xmlWriter.Flush();
        xmlWriter.Close();
    }
}
